<?php

namespace App\Http\Controllers\Site\Custom\Provider;
use App\B2BProject\Site\HelperControllerBuildTrait;
use App\B2BProject\Custom\Business\GoodsSpecification;
use Illuminate\Http\Request;
use App\Http\Requests;
use App\Http\Controllers\Controller;

/**
 * Class GoodsSpecController
 *
 * @package App\Http\Controllers\Site\Custom\Provider
 */
class GoodsSpecController extends Controller
{
    use HelperControllerBuildTrait;
    public function getIndex(Request $request)
    {
        if (is_null($ids = $request->query('specids', null))) {
            return response()->json([
                'data' => [
                    'specName' => ['', '', '', '', ''],
                    'specData' => [
                    ],
                ],
            ]);
        }

        $specs = GoodsSpecification::whereIn('id', $ids)->get();
        $goods = $specs->first()->goods;

        return response()->json([
            'data' => [
                'specName' => [
                    $goods->spec_first_name,
                    $goods->spec_second_name,
                    $goods->spec_third_name,
                    $goods->spec_forth_name,
                    $goods->spec_fifth_name,
                ],
                'specData' => call_user_func_array(function($specs) {
                    $data = [];
                    foreach ($specs as $spec) {
                        $data[] = [
                            'id' => $spec->id,
                            'spec_1' => $spec->spec_first,
                            'spec_2' => $spec->spec_second,
                            'spec_3' => $spec->spec_third,
                            'spec_4' => $spec->spec_forth,
                            'spec_5' => $spec->spec_fifth,
                            'price' => $spec->original_price,
                            'stock' => $spec->stock,
                            'articleNum' => $spec->cn
                        ];
                    }

                    return $data;
                }, [$specs])
            ],
        ]);
    }

    public function deleteIndex(Request $request)
    {
        $id = $request->input('specid');

        $spec = GoodsSpecification::findOrFail($id);

        $goods = $spec->first()->goods;

        $spec->delete();

        return response()->json([
            'data' => [
                'specName' => [
                    $goods->spec_first_name,
                    $goods->spec_second_name,
                    $goods->spec_third_name,
                    $goods->spec_forth_name,
                    $goods->spec_fifth_name,
                ],
                'specData' => call_user_func_array(function($specs) {
                    $data = [];
                    foreach ($specs as $spec) {
                        $data[] = [
                            'id' => $spec->id,
                            'spec_1' => $spec->spec_first,
                            'spec_2' => $spec->spec_second,
                            'spec_3' => $spec->spec_third,
                            'spec_4' => $spec->spec_forth,
                            'spec_5' => $spec->spec_fifth,
                            'price' => $spec->original_price,
                            'stock' => $spec->stock,
                            'articleNum' => $spec->cn
                        ];
                    }

                    return $data;
                }, [$goods->goodsSpecification])
            ],
        ]);
    }
}
